package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.AddressMapper;
import com.mysterymall.Mapper.UserMapper;
import com.mysterymall.Model.Entity.Address;
import com.mysterymall.Model.Entity.User;
import com.mysterymall.Service.AddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/12/08  09:56
 */
@Service
public class AddressServiceImpl implements AddressService {

    private final AddressMapper addressMapper;


    private final UserMapper userMapper;

    /**
     * 构造器注入
     *
     * @param addressMapper 地址mapper
     */
    public AddressServiceImpl(AddressMapper addressMapper, UserMapper userMapper) {
        this.addressMapper = addressMapper;
        this.userMapper = userMapper;
    }


    @Override
    @Transactional
    public Result addAddress(Address address) {

        boolean is_default = false;
        if (address.getId() != null) {
            if ("1".equals(address.getId())) {
                is_default = true;
            }
            address.setId(null);
        }
        // 当前登录
        User user = userMapper.selectById(StpUtil.getLoginId().toString());

        // 查询当前用户的地址总数
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, user.getId());
        Long count = addressMapper.selectCount(wrapper);

        if (count > 10) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.addError");
        }

        address.setUserId(user.getId());
        addressMapper.insert(address);
        if (is_default) {
            user.setAddress(address.getId());
            userMapper.updateById(user);
        }
        return Result.success("message.addSuccess");
    }

    @Override
    public Result deleteAddress(String id) {
        // 当前登录
        User user = userMapper.selectById(StpUtil.getLoginId().toString());
        //  查询地址
        Address address1 = addressMapper.selectById(id);
        if (address1 == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.deleteError");
        }
        if (!address1.getUserId().equals(user.getId())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }
        addressMapper.deleteById(id);
        if (user.getAddress().equals(id)) {
            LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Address::getUserId, user.getId());
            Address address = addressMapper.selectOne(wrapper);
            if (address != null) {
                user.setAddress(null);
                userMapper.updateById(user);
            }
        }

        return Result.success("message.deleteSuccess");
    }

    @Override
    public Result updateAddress(Address address) {

        if (address.getId() == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.updateError");
        }
        // 查询地址
        Address address1 = addressMapper.selectById(address.getId());
        if (address1 == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.updateError");
        }
        if (!address1.getUserId().equals(StpUtil.getLoginId().toString())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }
        // 当前登录
        User user = userMapper.selectById(StpUtil.getLoginId().toString());
        address.setUserId(user.getId());
        addressMapper.updateById(address);
        return Result.success("message.updateSuccess");
    }

    @Override
    public Result queryAddress() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        // 当前登录
        User user = userMapper.selectById(StpUtil.getLoginId().toString());
        wrapper.eq(Address::getUserId, user.getId());
        List<Address> addresses = addressMapper.selectList(wrapper);
        return Result.success("message.querySuccess", addresses);
    }

    @Override
    public Result queryOneAddress(String id) {
        // 查询
        Address address = addressMapper.selectById(id);
        if (address == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.queryError");
        }
        // 地址是否属于当前用户
        if (!address.getUserId().equals(StpUtil.getLoginId().toString())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }
        return Result.success("message.querySuccess",address);
    }

    @Override
    public Result adminQueryOneAddress(String orderId) {

        // 查询地址
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, orderId);

        Address address = addressMapper.selectOne(wrapper);

        return new Result(HttpStatus.SUCCESS,"message.querySuccess",address);
    }

    @Override
    public Result queryOneAddressAdmin(String id) {
        // 查询
        Address address = addressMapper.selectById(id);
        if (address == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.queryError");
        }
        return Result.success("message.querySuccess",address);
    }

    @Override
    @Transactional
    public Result addAddressAdmin(Address address) {


        if(address.getUserId() == null){
            return new Result(HttpStatus.BAD_NOT_PASS,"message.addError");
        }

        addressMapper.insert(address);

        User user = userMapper.selectById(address.getUserId());

        user.setAddress(address.getId());

        userMapper.updateById(user);

        return new Result(HttpStatus.SUCCESS,"message.addSuccess");
    }

    @Override
    public Result updateAddressAdmin(Address address) {
        if(address.getId() == null){
            return new Result(HttpStatus.BAD_NOT_PASS,"message.updateError");
        }
        addressMapper.updateById(address);
        return new Result(HttpStatus.SUCCESS,"message.updateSuccess");
    }
}
